import { Packet } from "./AChannel";

export class MessageSerializeHelper {
    public static readonly PbMaxOpcode = 40000;

    public static readonly JsonMinOpcode = 51000;

    public static DeserializeFrom(opcode: number, type: any, memoryStream: DataView) {
        // if (opcode < PbMaxOpcode) {
        //     return ProtobufHelper.FromStream(type, memoryStream);
        // }

        // if (opcode >= JsonMinOpcode) {
        //     return JsonHelper.FromJson(type, memoryStream.GetBuffer().ToStr((int)memoryStream.Position, (int)(memoryStream.Length - memoryStream.Position)));
        // }
        // #if NOT_CLIENT
        //         return MongoHelper.FromStream(type, memoryStream);
        // #else
        //         throw new Exception($"client no message: {opcode}");
        // #endif
        return undefined;
    }
    public static SerializeTo(opcode: number, obj: any, memoryStream: DataView) {
        //         if (opcode < PbMaxOpcode)
        //         {
        //             ProtobufHelper.ToStream(obj, memoryStream);
        //             return;
        //         }

        //         if (opcode >= JsonMinOpcode)
        //         {
        //             string s = JsonHelper.ToJson(obj);
        //             byte[] bytes = s.ToUtf8();
        //             memoryStream.Write(bytes, 0, bytes.Length);
        //             return;
        //         }
        // #if NOT_CLIENT
        //         MongoHelper.ToStream(obj, memoryStream);
        // #else
        //         throw new Exception($"client no message: {opcode}");
        // #endif
    }
    public static GetStream(count = 0): DataView {
        let stream: DataView;
        stream = new DataView(new ArrayBuffer(count));
        return stream;
    }
    // public static MessageToStream(message: number, count = 0): [number, DataView] {
    //     // let stream = this.GetStream(Packet.OpcodeLength + count);

    //     // let opcode = OpcodeTypeComponent.Instance.GetOpcode(message.GetType());

    //     // stream.Seek(Packet.OpcodeLength, SeekOrigin.Begin);
    //     // stream.SetLength(Packet.OpcodeLength);

    //     // stream.GetBuffer().WriteTo(0, opcode);

    //     // MessageSerializeHelper.SerializeTo(opcode, message, stream);

    //     // stream.Seek(0, SeekOrigin.Begin);
    //     // return (opcode, stream);
    //     return [undefined, undefined];
    // }
    public static MessageToStream1(message: any, count: number=0): [number, DataView] {
        return [undefined, undefined];
    }
    public static MessageToStream2(actorId: Long, message: any, count: number = 0): [number, DataView] {
        // int actorSize = sizeof(long);
        // MemoryStream stream = GetStream(actorSize + Packet.OpcodeLength + count);

        // ushort opcode = OpcodeTypeComponent.Instance.GetOpcode(message.GetType());

        // stream.Seek(actorSize + Packet.OpcodeLength, SeekOrigin.Begin);
        // stream.SetLength(actorSize + Packet.OpcodeLength);

        // // 写入actorId
        // stream.GetBuffer().WriteTo(0, actorId);
        // stream.GetBuffer().WriteTo(actorSize, opcode);

        // MessageSerializeHelper.SerializeTo(opcode, message, stream);

        // stream.Seek(0, SeekOrigin.Begin);
        return [undefined, undefined];
    }
}